using System.Reflection;
using Asp.Versioning;
using Asp.Versioning.Builder;
using Microsoft.Extensions.Options;
using MinimalAPI.Sample.Extensions;
using Swagger.Shared;
using Swashbuckle.AspNetCore.SwaggerGen;
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
builder.Services.AddSwaggerGen(options =>
{
    options.OperationFilter<SwaggerDefaultValues>();
});
builder.Services.AddApiVersioning(options =>
{
    options.AssumeDefaultVersionWhenUnspecified = true; //当未指定版本时，使用默认版本
    options.DefaultApiVersion = new ApiVersion(1, 0); //默认版本
    options.ReportApiVersions = true; //返回支持的版本信息
    options.ApiVersionReader = new UrlSegmentApiVersionReader();
    options.Policies.Sunset( 1.0 ) //配置Sunset
        .Effective( 2025, 6, 1 )
        .Link( "https://docs.api.com/policy.html?api-version=1.0" )
        .Title( "API Policy" )
        .Type( "text/html" );
    
    options.Policies.Sunset(2.0)
        .Effective(2025, 12, 1)
        .Link("https://docs.api.com/policy.html?api-version=2.0")
        .Title("API Policy")
        .Type("text/html");
}).AddApiExplorer(options =>
{
    options.GroupNameFormat = "'v'V";
    options.SubstituteApiVersionInUrl = true;
});
builder.Services.AddEndpoints(Assembly.GetExecutingAssembly());

var app = builder.Build();

ApiVersionSet apiVersionSet = app.NewApiVersionSet()
    .HasApiVersion(new ApiVersion(1)) 
    .HasApiVersion(new ApiVersion(2))
    .ReportApiVersions()
    .Build();

RouteGroupBuilder versionedGroup = app
    .MapGroup("api/v{version:apiVersion}")
    .WithApiVersionSet(apiVersionSet);

app.MapEndpoints(versionedGroup);
app.UseSwaggerWithUi();
app.UseHttpsRedirection();
app.Run();